<template>
    <p-card>
        <p-page-header
                v-if="pageState==1"
                style="border: 1px solid rgb(235, 237, 240)"
                title="首检确认操作"
        >
            <template slot="extra">
                <p-button @click="backToIndex">取消</p-button>
            </template>
        </p-page-header>
        <p-page-header
                v-if="pageState==2"
                style="border: 1px solid rgb(235, 237, 240)"
                title="首检复核操作"
        >
            <template slot="extra">
                <p-button @click="backToIndex">取消</p-button>
            </template>
        </p-page-header>
        <p-page-header
                v-if="pageState==3"
                style="border: 1px solid rgb(235, 237, 240)"
                title="首检确认操作（线材预制）"
        >
            <template slot="extra">
                <p-button @click="backToIndex">取消</p-button>
            </template>
        </p-page-header>
        <p-page-header
                v-if="pageState==4"
                style="border: 1px solid rgb(235, 237, 240)"
                title="首检复核操作（线材预制）"
        >
            <template slot="extra">
                <p-button @click="backToIndex">取消</p-button>
            </template>
        </p-page-header>
        <p-form-model
                ref="baseInfoData"
                :model="baseInfoData"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol"
        >

            <p-collapse v-model="activeKey" >
                <p-collapse-panel key="1" header="基础信息"  >

                    <p-row :gutter="24" >

                        <p-col :span="8">
                            <p-form-model-item ref="inspectBillno" label="送检单号" prop="inspectBillno">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.inspectBillno"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="productionBillno" label="生产工单" prop="productionBillno">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.productionBillno"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="processBillno" label="工序工单" prop="processBillno">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.processBillno"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="mcode" label="产品编号" prop="mcode">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.mcode"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="mname" label="产品名称" prop="mname">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.mname"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="processTwo" label="二级工序" prop="processTwo">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.processTwo"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="linename" label="线体" prop="linename">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.linename"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="deviceCode" label="设备编码" prop="deviceCode">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.deviceCode"
                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="inspectTimes" label="检验次数" prop="inspectTimes">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.inspectTimes"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="confirmTime" label="确认时间" prop="confirmTime">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.confirmTime"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8">
                            <p-form-model-item ref="confirmBy" label="确认人员" prop="confirmBy">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.confirmBy"

                                />
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="8" v-if="pageState==2||pageState==1">
                            <p-form-model-item ref="sn" label="sn" prop="sn">
                                <p-input
                                        readOnly
                                        v-model="baseInfoData.sn"

                                />
                            </p-form-model-item>
                        </p-col>

                        <p-col :span="8" v-if="pageState==2||pageState==4">
                            <p-form-model-item ref="isSimple" label="是否抽检" prop="isSimple">
                                <p-select  v-model="baseInfoData.isSimple">
                                    <p-select-option value=1>是</p-select-option>
                                    <p-select-option value=2>否</p-select-option>
                                </p-select>
                            </p-form-model-item>
                        </p-col>
                        <!-- <p-col :span="24">
                                  <p-form-model-item :wrapper-col="{offset: 2 }">
                                    <p-button type="primary" @click="onSubmit">Create</p-button>
                                    <p-button style="margin-left: 10px;" @click="resetForm">Reset</p-button>
                                  </p-form-model-item>
                        </p-col>-->
                    </p-row>

                </p-collapse-panel>

                <p-collapse-panel key="2" header="检验项目">
                    <p-row v-if="pageState==3||pageState==4">
                        <p-select
                                showSearch
                                v-model="currentLine"
                                @change="currentLineChange"
                                style="width: 200px"
                        >
                            <p-select-option
                                    v-for="(item,index) in selectLineList"
                                    :key="index"
                                    :value="item"
                            >{{item}}</p-select-option>
                        </p-select>
                    </p-row>


                    <p-table v-if="pageState==1||pageState==2" :data-source="checkProjectData1" :columns="checkProjectColumns1"  bordered>

                    </p-table>

                    <p-table v-if="pageState==3||pageState==4" :data-source="checkProjectData2" :columns="checkProjectColumns2"   bordered>

                    </p-table>

                </p-collapse-panel>


                <p-collapse-panel key="3" header="流程处理">
                    <p-alert message="审批记录" type="info" style="margin-top: 16px;margin-bottom: 16px"/>
                    <p-table  :data-source="baseInfoData.mesQcApproveRecordList" :columns="approveRecordColumns"  bordered>

                    </p-table>

                </p-collapse-panel>


            </p-collapse>

            <p-alert message="流程处理" type="info" style="margin-top: 16px;margin-bottom: 16px"/>
            <p-form-model
                    ref="processProcessingData"
                    :label-col="{span:2}"
                    :wrapper-col="{span:12}"
                    :model="baseInfoData"
            >
                <p-row :gutter="24">
                    <p-col :span="24">
                        <p-form-model-item ref="operation" label="操作" prop="operation" >
                            <p-radio-group v-model="baseInfoData.operation" style="width: 50%">
                                <p-radio-group v-model="baseInfoData.operation" style="width: 50%" @change="operationChange">
                                    <p-radio value="1">通过</p-radio>
                                    <p-radio value="2">驳回</p-radio>
                                </p-radio-group>
                                <p-button type="danger" style="margin-right: 16px;" @click="testTest">异常记录</p-button>
                                <p-button type="danger">产品锁定</p-button>
                            </p-radio-group>
                        </p-form-model-item>


                    </p-col>
                    <p-col :span="24">
                        <p-col :span="24" v-if="baseInfoData.operation==2">
                            <p-form-model-item
                                    ref="inspectFailWs"
                                    label="检验不合格工站"
                                    prop="inspectFailWs"
                            >
                                <p-select

                                        v-model="baseInfoData.inspectFailWs"
                                        mode="multiple"
                                >
                                    <p-select-option
                                            v-for="(item,index) in workStationOptions"
                                            :key="index"
                                            :value="item.workStationCode"
                                    >{{item.workStation}}</p-select-option>
                                </p-select>
                            </p-form-model-item>
                        </p-col>
                        <p-col :span="24">
                            <p-form-model-item ref="remark" label="备注" prop="remark">
                                <p-input
                                        v-model="baseInfoData.remark"
                                        type="textarea"
                                        @blur="
          () => {
            $refs.remark.onFieldBlur();
          }
        "
                                />
                            </p-form-model-item>
                        </p-col>

                        <p-col :span="24">
                            <p-form-model-item ref="currentProcessor" label="当前处理人" prop="currentProcessor">
                                <p-input
                                        readOnly
                                        style="width: 80%"
                                        v-model="baseInfoData.currentHandler"
                                />

                                <p-popconfirm
                                        style="margin-left: 16px;"
                                        title="确定要提交吗？"
                                        ok-text="确定"
                                        cancel-text="取消"
                                        @confirm="submitData"
                                        @cancel="cancel"
                                >
                                    <p-button type="primary" >提交</p-button>
                                </p-popconfirm>

                            </p-form-model-item>
                        </p-col>
                    </p-col>
                </p-row>

            </p-form-model>

        </p-form-model>



    </p-card>

</template>

<script>
    import {getAction,postAction,putAction} from "../../../api/manage";
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import {
        initDictOptions,
        filterMultiDictText,
    } from "@/components/dict/JDictSelectUtil";
    import MesMfProcessCriticalComponentBindingList from "./MesMfProcessCriticalComponentBindingList";


    export default {
        name: "MesQcFirstCheckCFOpe",
        components: {MesMfProcessCriticalComponentBindingList},
        mixins: [JeecgListMixin],
        props:['mqVisible','mqLoad','mqData'],
        data(){
            return {
                labelCol: { span: 8 },
                wrapperCol: { span: 14 },
                pageState:1,
                activeKey:['1','2','3'],

                workStationOptions: [

                ],
                workStationMap:[],

                currentLine:'',
                approveRecordData:[],
                approveRecordColumns:[
                    {
                        title: "序号",
                        dataIndex: "",
                        key: "rowIndex",
                        width: 60,
                        align: "center",
                        customRender: function (t, r, index) {
                            return parseInt(index) + 1;
                        },
                    },
                    {
                        title: "时间",
                        dataIndex: "createTime",
                        customRender: (text) => {
                            console.log("text:")
                            console.log(text)
                            return this.formatDate(text+'',"YYYY-MM-DD HH:mm:ss");
                        },
                    },
                    {
                        title: "操作人员",
                        dataIndex: "opeBy",
                    },
                    {
                        title: "操作内容",
                        dataIndex: "remark",
                    },
                    {
                        title: "检验不合格工站",
                        dataIndex: "inspectFailName",
                    },
                    {
                        title: "状态",
                        dataIndex: "opeContent",
                    },
                ],


                selectLineList:[],
                checkProjectData2:[],
                firstCheckProjectData2:[],
                checkProjectColumns2:[
                    {
                        title: "序号",
                        dataIndex: "",
                        key: "rowIndex",
                        width: 60,
                        align: "center",
                        customRender: function (t, r, index) {
                            return parseInt(index) + 1;
                        },
                    },
                    {
                        title: "工站",
                        dataIndex: "workStation",
                    },
                    {
                        title: "检验项目",
                        dataIndex: "inspectProject",
                    },
                    {
                        title: "检验标准",
                        dataIndex: "inspectStandard",
                    },
                    {
                        title: "值类型",
                        dataIndex: "valueType",
                        customRender: (text) => {
                            if (!text) {
                                return "";
                            } else {
                                return  text=="1"?"计量型":"计数型";
                            }
                        },
                    },
                    {
                        title: "检验手段",
                        dataIndex: "inspectMethod",
                        customRender:(text)=>{
                            if(!text){
                                return "";
                            }else{
                                if(this.methodMap[text]){
                                    return this.methodMap[text].title;
                                }
                                return  "";
                            }
                        }
                    },
                    {
                        title: "标准值",
                        dataIndex: "standardValue",
                    },
                    {
                        title: "上限值",
                        dataIndex: "upperValue",
                    },
                    {
                        title: "下限值",
                        dataIndex: "downValue",
                    },
                    {
                        title: "参数",
                        dataIndex: "paramValue",
                    },
                    {
                        title: "实测值",
                        dataIndex: "measuredValue",
                    },
                    {
                        title: "单项判定",
                        dataIndex: "individualJudgment",
                        customRender: (text) => {
                            if (!text) {
                                return "NG";
                            } else {
                                return  text==1?"Pass":"NG";
                            }
                        },
                    },
                    {
                        title: "检验人",
                        dataIndex: "judge",
                    },
                ],
                checkProjectData1:[],
                checkProjectColumns1:[
                    {
                        title: "序号",
                        dataIndex: "",
                        key: "rowIndex",
                        width: 60,
                        align: "center",
                        customRender: function (t, r, index) {
                            return parseInt(index) + 1;
                        },
                    },
                    {
                        title: "工站",
                        dataIndex: "workStation",
                    },
                    {
                        title: "检验项目",
                        dataIndex: "inspectProject",
                    },
                    {
                        title: "检验标准",
                        dataIndex: "inspectStandard",
                    },
                    {
                        title: "值类型",
                        dataIndex: "valueType",
                        customRender: (text) => {
                            if (!text) {
                                return "";
                            } else {
                                return text=="1"?"计量型":"计数型";
                            }
                        },
                    },
                    {
                        title: "检验手段",
                        dataIndex: "inspectMethod",
                        customRender:(text)=>{
                            if(!text){
                                return "";
                            }else{
                                if(this.methodMap[text]){
                                    return this.methodMap[text].title;
                                }
                                return  "";
                            }
                        }
                    },
                    {
                        title: "标准值",
                        dataIndex: "standardValue",
                    },
                    {
                        title: "上限值",
                        dataIndex: "upperValue",
                    },
                    {
                        title: "下限值",
                        dataIndex: "downValue",
                    },
                    {
                        title: "参数",
                        dataIndex: "paramValue",
                    },
                    {
                        title: "实测值",
                        dataIndex: "measuredValue",
                    },
                    {
                        title: "单项判定",
                        dataIndex: "individualJudgment",
                        customRender: (text) => {
                            if (!text) {
                                return "NG";
                            } else {
                                return  text==1?"Pass":"NG";
                            }
                        },
                    },
                    {
                        title: "检验人",
                        dataIndex: "judge",
                    },
                ],

                baseInfoData: {
                    inspectBillno: "001", //送检单号
                    productionBillno: "", //生产工单
                    processBillno: "001", //工序工单
                    mcode: "", //产品编号
                    mname: "", //产品名称
                    processTwo: "", //二级工序
                    linename: "", //线体
                    deviceCode: "", //设备编码
                    inspectTimes: "3", //检验次数
                    confirmTime: "2020-01-01", //确认时间
                    confirmBy: "", //确认人员
                    sn: "", //sn
                },

                rules: {
                    inspectBillno: [
                        {
                            required: true,
                            message: "Please input Activity name",

                        },
                    ],
                    productionBillno: [
                        {
                            required: true,
                            message: "Please input Activity name",

                        },
                    ],
                    processBillno: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    mcode: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    mname: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    processTwo: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    linename: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    deviceCode: [
                        {
                            required: true,
                            message: "Please input Activity name",
                        },
                    ],
                    inspectTimes: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    confirmTime: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    confirmBy: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],
                    sn: [
                        {
                            required: true,
                            message: "Please input Activity name",
                            trigger: "blur",
                        },
                    ],

                    isSimple: [
                        {
                            required: true,
                            message: "请选择是否抽检",
                        },
                    ],
                },
            };

        },

        computed: {

        },

        created() {
            this.getMethodList();
            this.mqListener();
        },

        methods: {
            mqListener(){
                console.log(this.mqVisible)
                console.log(this.mqLoad)
                console.log(this.mqData)

                this.initData(this.mqData);
            },
            backToIndex(){
                let mqData = this.mqData;
                this.$emit('update:mqVisible', 1);
                this.$emit('update:mqLoad', false);
                mqData.id = 0;
                mqData.isPallet = "";
                mqData.inspectStatus = "";
                this.$emit('update:mqData', mqData);
            },



            //生成checkProjectColumns2
            createCheckProjectColumns2(data){
                let columns=[
                    {
                        title: "序号",
                        dataIndex: "",
                        key: "rowIndex",
                        width: 60,
                        align: "center",
                        customRender: function (t, r, index) {
                            return parseInt(index) + 1;
                        },
                    },
                    {
                        title: "工站",
                        key:"工站",
                        dataIndex: "工站",
                    },
                    {
                        title: "线号",
                        key:"线号",
                        dataIndex: "线号",
                    },
                ];


                let used = new Set(["线号","检验结果","检验人","检验时间"]);


                data.forEach(item=>{

                    if(item.线号 == this.currentLine){
                        for(let attribute in item){
                            if(!used.has(attribute)&&attribute!='工站'){
                                let list = item[attribute];
                                let index = 0;
                                list.forEach(j=>{
                                    let children = [];

                                    if(j.valueType==1){
                                        children.push({title:'标准值',dataIndex:'list['+index+'].standardValues',key:'list['+index+'].standardValues'})
                                        children.push({title:'上限值',dataIndex:'list['+index+'].upperValue',key:'list['+index+'].upperValue'})
                                        children.push({title:'下限值',dataIndex:'list['+index+'].downValue',key:'list['+index+'].downValue'})
                                    }
                                    children.push({title:'实测值',dataIndex:'list['+index+'].value',key:'list['+index+'].value'})

                                    columns.push({title:j.inspectProject,children:children})

                                    index ++;
                                })
                            }
                        }
                    }

                });

                columns.push(
                    {
                        title: "检验结果",
                        key:"检验结果",
                        dataIndex: "检验结果",
                    },
                    {
                        title: "检验人",
                        key:"检验人",
                        dataIndex: "检验人",
                    },
                    {
                        title: "检验时间",
                        key:"检验时间",
                        dataIndex: "检验时间",
                    },
                )

                this.checkProjectColumns2 = columns;

                console.log(this.checkProjectColumns2);

            },

            operationChange(){
            },

            testTest(){
                let nameSet = new Set(['name']);

                let obj = {
                    name:'123',
                    age:14
                }

                let props = "";
                for(let p in obj) {
                    // 方法
                    // p 为属性名称，obj[p]为对应属性的值
                    props += p + "=" + obj[p] + "\t";
                    console.log(nameSet.has(p))
                }

                console.log(props)
            },

            //封装listToMap
            listToMap(list,name){
                let map = {};
                for(let i in list){
                    map[list[i][name]] = list[i];
                }
                return map;
            },
            currentLineChange(){

                let currentLine = this.currentLine;


                let param = {
                    id:this.baseInfoData.id,
                    wcCode:currentLine
                }
                postAction('/mes-quality/mesQcInspectTask/selectByLineNo',param).then(res=>{
                    if(res.success){
                        let data = res.data;
                        this.checkProjectData2 = data;
                    }else{
                        this.$message.warning(res.msg)
                    }
                })

            },
            getMethodList(){
                getAction('/basic-business/sys/dict/queryDictItemsByCode',{dictCode:'mes_inspect_method'}).then(res=>{
                    if(res.success){
                        this.methodList = res.data;
                        this.methodMap = this.listToMap(this.methodList,'value');
                    }
                })
            },
            //数据初始化
            initData(query){

                if(!query.id){
                    this.$message.warning("请通过首检任务清单选择单号进入！");
                }else{
                    let isPallet = query.isPallet;
                    let id = query.id;
                    let inspectStatus = query.inspectStatus;

                    if(isPallet==1){
                        if(inspectStatus==2){
                            this.pageState = 3;
                        }else{
                            this.pageState = 4;
                        }
                    }else{
                        if(inspectStatus==2){
                            this.pageState = 1;
                        }else{
                            this.pageState = 2;
                        }
                    }



                    getAction('/mes-quality/mesQcInspectTask/selectById',{id:id}).then(res=>{
                        if(res.success){
                            if(res.code!=0){
                                this.$message.warning(res.msg)
                            }
                            let data = res.data;
                            data.inspectFailWs = data.inspectFailWs?data.inspectFailWs.split(','):[];
                            this.workStationOptions = data.qualityWorkStation;
                            this.workStationMap = this.listToMap(data.qualityWorkStation,'workStationCode');

                            //workStationOptions
                            data.currentHandler = this.$store.state.user.name;
                            data.confirmBy = this.$store.state.user.name;

                            if(isPallet==1){
                               /* let mesQcPalletDtoList = data.mesQcPalletDtoList;
                                this.firstCheckProjectData2 = mesQcPalletDtoList;

                                let selectLineList = new Set();
                                this.firstCheckProjectData2.forEach(item=>{
                                    selectLineList.add(item.线号)
                                })*/

                                this.selectLineList = data.qualifiedPallet;
                                this.baseInfoData = data;
                                if(data.qualifiedPallet.length>0){
                                    this.currentLine = data.qualifiedPallet[0];
                                    this.currentLineChange();
                                }
                            }else{
                                this.checkProjectData1 = data.mesQcInspectTaskChildList;
                            }
                            this.baseInfoData = data;

                        }else{
                            this.$message.warning(res.msg);
                        }
                    })


                }
            },


            submitData(){
                let flag = true;
                let data = this.baseInfoData;

                if(data.inspectFailWs.length>0){
                    let result = "";
                    let nameResult = '';
                    data.inspectFailWs.forEach(item=>{
                        result= result+item+',';
                        nameResult = nameResult+this.workStationMap[item].workStation+',';
                    })
                    result = result.substring(0,result.length-1);
                    nameResult = nameResult.substring(0,nameResult.length-1);
                    data.inspectFailWs = result;
                    data.inspectFailName = nameResult;
                }else{
                    data.inspectFailWs = [];
                    data.inspectFailName = null;
                }
                if(this.pageState==2||this.pageState==4){
                    if(data.isSimple===''){
                        this.$message.error("请选择是否抽检!");
                    }
                }

                if(!data.operation||data.operation===""){

                    flag = false;
                    this.$message.error("请选择 通过/驳回 ！");
                }

                if(data.operation==2&&(!data.remark||data.remark=='')){
                    flag = false;
                    this.$message.error("驳回后必填驳回的原因（备注） ！");
                }

                if(data.operation==2&&(!data.inspectFailWs||data.inspectFailWs=='')){
                    flag = false;
                    this.$message.error("驳回后必选检验不合格工站 ！");
                }



                if(flag){
                    putAction('/mes-quality/mesQcInspectTask/updateFirstInspectIpqc',data).then(res=>{
                        if(res.success){
                            this.$message.success("提交成功!");
                            let mqData = this.mqData;
                            this.$emit('update:mqVisible', 1);
                            this.$emit('update:mqLoad', true);
                            mqData.id = 0;
                            mqData.isPallet = "";
                            mqData.inspectStatus = "";
                            this.$emit('update:mqData', mqData);
                        }else{
                            this.$message.error("提交失败!");
                            this.$message.warning(res.msg);
                        }
                    })
                }
            }



        },
    }


</script>

<style scoped>

</style>
